<?php

namespace app\api\service;

use HuaweiCloud\SDK\Core\Auth\GlobalCredentials;
use HuaweiCloud\SDK\Core\Exceptions\ConnectionException;
use HuaweiCloud\SDK\Core\Exceptions\RequestTimeoutException;
use HuaweiCloud\SDK\Core\Exceptions\ServiceResponseException;
use HuaweiCloud\SDK\Core\Http\HttpConfig;
use HuaweiCloud\SDK\Iam\V3\IamClient;
use HuaweiCloud\SDK\Iam\V3\Model\ListPermanentAccessKeysRequest;
use Monolog\Logger;
use think\Env;

// 华为视频点播
class HuaWeiVOD
{
    protected $ak = "";
    protected $sk = "";

    // https://developer.huaweicloud.com/endpoint
    protected $endpoint = "vod.cn-east-2.myhuaweicloud.com";
    // 华为云帐号ID
    protected $domainId = "";

    public function __construct()
    {
        $this->ak = Env::get('huawei.ak');
        $this->sk = Env::get('huawei.sk');
        $this->domainId = Env::get('huawei.domainId');


        $config = HttpConfig::getDefaultConfig();

        $config->setIgnoreSslVerification(true);
        $credentials = new GlobalCredentials($this->ak, $this->sk, $this->domainId);

        $iamClient = IamClient::newBuilder()
            ->withHttpConfig($config)
            ->withEndpoint($this->endpoint)
            ->withCredentials($credentials)
            ->withStreamLogger($stream = 'php://stdout', $logLevel = Logger::INFO)
            ->withFileLogger($logPath = './test_log.txt', $logLevel = Logger::INFO)
            ->build();


        $this->listPermanentAccessKeys($iamClient);
    }

    public function listPermanentAccessKeys($iamClient)
    {
        $listPermanentAccessKeysRequest = new ListPermanentAccessKeysRequest(array('userId' => "{your user id}"));
        try {
            $response = $iamClient->listPermanentAccessKeys($listPermanentAccessKeysRequest);
            echo "\n";
            echo $response;
        } catch (ConnectionException $e) {
            $msg = $e->getMessage();
            echo "\n" . $msg . "\n";
        } catch (RequestTimeoutException $e) {
            $msg = $e->getMessage();
            echo "\n" . $msg . "\n";
        } catch (ServiceResponseException $e) {
            echo "\n";
            echo $e->getHttpStatusCode() . "\n";
            echo $e->getRequestId() . "\n";
            echo $e->getErrorCode() . "\n";
            echo $e->getErrorMsg() . "\n";
        }
    }

    public function run()
    {

    }


}
